package com.examstack.management.controller.action;

import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.examstack.common.domain.question.CheckRecord;
import com.examstack.management.security.QuestionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.examstack.common.domain.exam.Message;
import com.examstack.common.domain.question.KnowledgePoint;
import com.examstack.common.domain.question.Question;
import com.examstack.common.domain.question.QuestionTag;
import com.examstack.common.util.file.FileUploadUtil;
import com.examstack.management.security.UserInfo;
import com.examstack.management.service.QuestionService;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

@Controller
public class QuestionAction {

	@Autowired
	private QuestionService questionService;

	/**
	 * 添加试题
	 * 
	 * @param question
	 * @return
	 */
	@RequestMapping(value = "/secure/question/question-add/{fieldId}", method = RequestMethod.POST)
	public @ResponseBody Message addQuestion(@RequestBody Question question,@PathVariable("fieldId") int fieldId) {
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Message message = new Message();
		//判断题目是否重复
		if(QuestionUtil.checkReaptQuestion(questionService,question.getQuestionContent().getTitle(),fieldId)){
			message.setResult("该试题已存在！");
			return message;
		}

		Gson gson = new Gson();
		question.setContent(gson.toJson(question.getQuestionContent()));
		question.setCreate_time(new Date());
		question.setCreator(userDetails.getUsername());
		question.setStatus(2);//默认添加为审核中
		question.setIs_visible(true);//默认设置为已开放
		try {
			questionService.addQuestion(question);
		} catch (Exception e) {
			message.setResult(e.getMessage());
			e.printStackTrace();
		}

		return message;
	}

	/**
	 * 获取试题的标签列表
	 * 
	 * @param questionId
	 * @return
	 */
	@RequestMapping(value = "/secure/question/question-tag/{questionId}", method = RequestMethod.GET)
	public @ResponseBody Message getQuestionTag(@PathVariable("questionId") int questionId) {
		Message message = new Message();
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<QuestionTag> tagList = questionService.getQuestionTagByQuestionIdAndUserId(questionId,
				userInfo.getUserid(), null);
		message.setObject(tagList);
		return message;
	}

	/**
	 * 为试题添加标签
	 * 
	 * @param questionId
	 * @param questionTagList
	 * @return
	 */
	@RequestMapping(value = "/secure/question/add-question-tag", method = RequestMethod.POST)
	public @ResponseBody Message addQuestionTag(@RequestBody int questionId,
			@RequestBody List<QuestionTag> questionTagList) {
		Message message = new Message();
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		try {
			questionService.addQuestionTag(questionId, userInfo.getUserid(), questionTagList);
		} catch (Exception e) {
			e.printStackTrace();
			message.setResult(e.getClass().getName());
		}

		return message;
	}

	/**
	 * 获取试题详细信息
	 * @param questionId
	 * @return
	 */
	@RequestMapping(value = "/secure/question/question-detail/{questionId}", method = RequestMethod.GET)
	public @ResponseBody Message getQuestionDetail(@PathVariable("questionId") int questionId) {
		Message message = new Message();
		//UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		try {
			Question question = questionService.getQuestionDetail(questionId, 0);
			message.setObject(question);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			message.setResult(e.getCause().getMessage());
		}
		return message;
	}

	/**
	 * 获取试题审核信息
	 * @param questionId
	 * @return
	 */
	@RequestMapping(value = "/secure/question/question-check-record/{questionId}", method = RequestMethod.GET)
	public @ResponseBody Message getCheckRecord(@PathVariable("questionId") int questionId) {
		Message message = new Message();
		try {
			List<CheckRecord> checkRecordList = questionService.getCheckRecordList(questionId);
			if(checkRecordList.size()<=0){
				message.setResult("暂无审核记录！");
				return message;
			}
			message.setObject(checkRecordList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			message.setResult(e.getCause().getMessage());
		}
		return message;
	}

	/**
	 * 修改试题知识点
	 * 
	 * @param questionId
	 * @param pointId
	 * @param questionTagList
	 * @return
	 */
	@RequestMapping(value = "/secure/question/question-update/{questionId}/{pointId}", method = RequestMethod.POST)
	public @ResponseBody Message updateQuestionKnowledge(@PathVariable int questionId, @PathVariable int pointId,
			@RequestBody List<QuestionTag> questionTagList) {

		Message message = new Message();
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Question question = new Question();
		question.setId(questionId);
		List<Integer> pointIdList = new ArrayList<Integer>();
		pointIdList.add(pointId);
		question.setPointList(pointIdList);
		try {
			questionService.updateQuestionPoint(question, userInfo.getUserid(), questionTagList);
		} catch (Exception e) {
			message.setResult(e.getClass().getName());
		}

		return message;
	}
	
	@RequestMapping(value = "/secure/question/question-update", method = RequestMethod.POST)
	public @ResponseBody Message updateQuestion(@RequestBody String jsonStr){
		Message msg = new Message();
		Gson gson = new Gson();
		JsonParser parser = new JsonParser();
		JsonElement element = parser.parse(jsonStr);
		List<QuestionTag> questionTagList = gson.fromJson(element.getAsJsonObject().get("tags"), new TypeToken<ArrayList<QuestionTag>>(){}.getType());
		Question question = gson.fromJson(element.getAsJsonObject().get("question"), Question.class);
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		try {
			CheckRecord checkRecord =new CheckRecord();
			if(question.getOldStatus()!=question.getStatus()){//状态更改 记录状态
				checkRecord.setQuestionId(question.getId());
				checkRecord.setCheckTime(new Date());
				checkRecord.setCheckMan(userInfo.getUserid());
				checkRecord.setNewStatus(question.getStatus());
				checkRecord.setOldStatus(question.getOldStatus());
			}else{//状态未更改  则记录是否异议
				checkRecord.setQuestionId(question.getId());
				checkRecord.setCheckTime(new Date());
				checkRecord.setCheckMan(userInfo.getUserid());
				checkRecord.setNewStatus(10);//无异议
				checkRecord.setOldStatus(11);//有异议
                question.setDissent(10);
			}

			questionService.updateQuestion(checkRecord,question, questionTagList);
			
		} catch (Exception e) {
			e.printStackTrace();
			msg.setResult(e.getCause().getMessage());
		}
		//TO-DO:需要提交到数据库，保证在事务中提交
		return msg;
	}

	@RequestMapping(value = "/secure/question/get-knowledge-point/{fieldId}", method = RequestMethod.GET)
	public @ResponseBody Message getQuestionPointByFieldId(@PathVariable int fieldId) {
		Message message = new Message();
		HashMap<Integer, String> pointMap = new HashMap<Integer, String>();
		List<KnowledgePoint> pointList = questionService.getKnowledgePointByFieldId(fieldId, null);
		for (KnowledgePoint point : pointList) {
			pointMap.put(point.getPointId(), point.getPointName());
		}
		message.setObject(pointMap);
		return message;
	}

	@RequestMapping(value = "/secure/question/update-question-status/{questionId}-{oldstatus}-{status}-{visible}", method = RequestMethod.GET)
	public @ResponseBody Message deleteQuestion(Model model, @PathVariable("questionId") int questionId
			, @PathVariable("status") int status
			, @PathVariable("oldstatus") int oldStatus
			, @PathVariable("visible") int visible) {

		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Message message = new Message();
		Question question = new Question();
		try {
			//questionService.deleteQuestionByQuestionId(questionId);
			if(status<1||status>7){
				message.setResult("试题状态错误！");
				return message;
			}
			question.setId(questionId);
			question.setStatus(status);
			if(visible==0){
				question.setIs_visible(false);
			}else{
				question.setIs_visible(true);
			}

			CheckRecord checkRecord =new CheckRecord();
			checkRecord.setQuestionId(question.getId());
			checkRecord.setCheckTime(new Date());
			checkRecord.setCheckMan(userInfo.getUserid());
			checkRecord.setNewStatus(status);
			checkRecord.setOldStatus(oldStatus);

			questionService.updateQuestionStatus(checkRecord,question);
		} catch (Exception ex) {
			message.setResult(ex.getClass().getName());
		}

		return message;
	}

	@RequestMapping(value = "/secure/upload-uploadify-img", method = RequestMethod.POST)
	public @ResponseBody String uploadImg(HttpServletRequest request, HttpServletResponse response) {
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<String> filePathList = new ArrayList<String>();
		try {
			filePathList = FileUploadUtil.uploadImg(request, response, userInfo.getUsername(),"question");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		if (filePathList.size() == 0) {
			return "系统错误";
		}

		return filePathList.get(0);
	}

	@RequestMapping(value = "/secure/upload-uploadify", method = RequestMethod.POST)
	public @ResponseBody String uploadFile(HttpServletRequest request, HttpServletResponse response) {
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<String> filePathList = new ArrayList<String>();
		try {
			String function = "question";
			filePathList = FileUploadUtil.uploadFile(request, response, userInfo.getUsername(), function);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		if (filePathList.size() == 0) {
			return "系统错误";
		}

		return filePathList.get(0);
	}
	
	@RequestMapping(value = "/secure/question-import/{id}", method = RequestMethod.POST)
	public @ResponseBody Message courseImport(@RequestBody String filePath, @PathVariable("id") int id) {
		Message message = new Message();
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();

		try{
			questionService.uploadQuestions(filePath, userInfo.getUsername(),id);
		}catch(RuntimeException e){
			message.setResult(e.getClass().getName() + ":" + e.getMessage());
			message.setMessageInfo(e.getMessage());
		}
		
		return message;
	}

	@RequestMapping(value = "/secure/question/question-update-visible", method = RequestMethod.POST)
	public @ResponseBody Message updateQuestionVisible(@RequestBody String jsonStr){
		Message msg = new Message();
		Gson gson = new Gson();
		JsonParser parser = new JsonParser();
		JsonElement element = parser.parse(jsonStr);
		List<Question> questionList = gson.fromJson(element.getAsJsonObject().get("questionList"), new TypeToken<List<Question>>(){}.getType());
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		try {

			for(Question question:questionList) {
				questionService.updateQuestionStatus(null,question);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg.setResult(e.getCause().getMessage());
		}
		return msg;
	}

}
